package com.cantierinformatici.mygym;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.content.Context;

import android.view.View.OnClickListener;
import android.content.Intent;

public class HomeActivity extends SherlockActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		ActionBar actionBar = getSupportActionBar();
		// actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(false);

		setAddButtons();

		ListLayout lista = (ListLayout) findViewById(R.id.home_box_list);
		lista.setList(new HomeListAdapter(getApplication()));

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.home, menu);
		getSupportMenuInflater().inflate(R.menu.home, menu);
		return true;
	}

	public void setAddButtons() {
		// ADD ATTREZZO
		RelativeLayout btnAddAttrezzo = (RelativeLayout) findViewById(R.id.btn_add_attrezzo);
		btnAddAttrezzo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplication(),
						com.cantierinformatici.mygym.AttrezzoActivity.class);
				intent.setAction(Intent.ACTION_INSERT);
				startActivity(intent);
			}
		});

		// ADD MUSCOLO
		RelativeLayout btnAddMuscolo = (RelativeLayout) findViewById(R.id.btn_add_muscolo);
		btnAddMuscolo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplication(),
						com.cantierinformatici.mygym.MuscoloActivity.class);
				intent.setAction(Intent.ACTION_INSERT);
				startActivity(intent);
			}
		});

		// ADD Esercizio
		RelativeLayout btnAddEsercizio = (RelativeLayout) findViewById(R.id.btn_add_esercizio);
		btnAddEsercizio.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplication(),
						com.cantierinformatici.mygym.EsercizioActivity.class);
				intent.setAction(Intent.ACTION_INSERT);
				startActivity(intent);
			}
		});
	}

	private class HomeListAdapter extends BaseAdapter {
		
		private int[] titoli = new int[] { R.string.home_list_attivita,
				R.string.home_list_pesi, R.string.home_list_attrezzi,
				R.string.home_list_esercizi, R.string.home_list_muscoli };
		
		private int[] icone = new int[] { R.drawable.ic_attivita,
				R.drawable.ic_peso, R.drawable.ic_attrezzo,
				R.drawable.ic_esercizio, R.drawable.ic_muscolo };
		
		private OnClickListener[] clicks = new OnClickListener[] {
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						startActivity(new Intent(getApplicationContext(),
								AttrezzoActivity.class));
					}
				}, new OnClickListener() {
					@Override
					public void onClick(View v) {
						startActivity(new Intent(getApplicationContext(),
								AttrezzoActivity.class));
					}
				}, new OnClickListener() {
					@Override
					public void onClick(View v) {
						startActivity(new Intent(getApplicationContext(),
								AttrezziActivity.class));
					}
				}, new OnClickListener() {
					@Override
					public void onClick(View v) {
						startActivity(new Intent(getApplicationContext(),
								EserciziActivity.class));
					}
				}, new OnClickListener() {
					@Override
					public void onClick(View v) {
						startActivity(new Intent(getApplicationContext(),
								MuscoliActivity.class));
					}
				} };

		public HomeListAdapter(Context context) {
			super();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View item = convertView;
			if (item == null) {
				item = getLayoutInflater().inflate(R.layout.home_list_item,
						parent);
			}

			TextView titoloView = (TextView) item
					.findViewById(R.id.home_list_text);
			ImageView iconaView = (ImageView) item
					.findViewById(R.id.home_list_image);
			RelativeLayout layout = (RelativeLayout) item
					.findViewById(R.id.home_listitem_lay);
			layout.setOnClickListener((android.view.View.OnClickListener) clicks[position]);
			titoloView.setText(titoli[position]);
			iconaView.setImageResource(icone[position]);
			return item;
		}

		@Override
		public int getCount() {

			return titoli.length;
		}

		@Override
		public Object getItem(int position) {

			return titoli[position];
		}

		@Override
		public long getItemId(int position) {

			return 0;
		}

	}

}
